package com.dhx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dhx.common.CustomException;
import com.dhx.domain.Setmeal;
import com.dhx.dao.SetmealDao;
import com.dhx.domain.SetmealDish;
import com.dhx.dto.DishDto;
import com.dhx.dto.SetmealDto;
import com.dhx.service.ISetmealDishService;
import com.dhx.service.ISetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author 邓鹄翔
 * @since 2022-03-08
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements ISetmealService {
    @Autowired
    private ISetmealService setmealService;
    @Autowired
    private ISetmealDishService setmealDishService;

    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     *
     * @param setmealDto
     */
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void removeWithDish(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> setmealwrapper = new LambdaQueryWrapper<>();
        setmealwrapper.in(Setmeal::getId, ids);
        setmealwrapper.eq(Setmeal::getStatus,1);
        int count = this.count(setmealwrapper);
        if (count > 0) {
            throw new CustomException("套餐正在售卖中，不能删除");//已经关联菜品，抛出一个业务异常
        }
        //如果可以删除，先删除套餐表中的数据---setmeal
        this.removeByIds(ids);

        LambdaQueryWrapper<SetmealDish> setmealDishwrapper = new LambdaQueryWrapper<>();
        setmealDishwrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(setmealDishwrapper);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithSetmeaDto(Long id) {


//        Dish dish =  this.getById(id);
//        DishDto dishDto = new DishDto();
//        BeanUtils.copyProperties(dish,dishDto);
//        //查询当前菜品对应的口味信息，从dish_flavor表查询
//        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(DishFlavor::getDishId,dish.getId());
//        List<DishFlavor> list = dishFlavorService.list(wrapper);
//        dishDto.setFlavors(list);
//        return dishDto;

        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        //查询
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 修改
     * @param setmealDto
     */
    @Override
    public void updateWithseteam(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        //清除数据
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //提交过来的数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((emp)->{
            emp.setSetmealId(setmealDto.getId());
            return emp;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }


}
